package com.crl.utils
{
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	/**
	 * 
	 * @author crl
	 * 
	 */	
	public class FMSUtil
	{
		/**
		 * 视频流的默认配置; 
		 */		
		public static var defaultVideoHeight:int=240;
		public static var defaultVideoWidth:int=320;
		public static var defaultVideoBandWidth:int = 64000;
		public static var defaultVideoQuality:int = 85;
		public static var defaultVideoFrameRate:int = 12;
		
		
		public function FMSUtil()
		{
		}
		
		/**
		 * 取得一个麦; 
		 * @return 
		 * 
		 */		
		static public function getMicrophone() : Microphone
        {
            var mic:Microphone= Microphone.getMicrophone();
            if (mic)
            {
                mic.rate = 22;
                mic.setSilenceLevel(0);
                mic.setUseEchoSuppression(true);
            }
            return mic;
        }
        
        /**
         *	取得一个视频; 
         * @return 
         * 
         */        
        static public function getCamera() : Camera
        {
            var camera:Camera = Camera.getCamera();
            if (camera)
            {
                camera.setMode(defaultVideoWidth, defaultVideoHeight, defaultVideoFrameRate);
                camera.setQuality(defaultVideoBandWidth,defaultVideoQuality);
                camera.setLoopback(true);
            }
            return camera;
        }
        
       /**
        *	创建一个流; 
        * @param conn
        * @param ns 旧的流,此处来关闭它.
        * @return 
        * 
        */        
       public static function createNetStream(conn:NetConnection, ns:NetStream = null) : NetStream
        {
            var stream:NetStream;
            destroyStream(ns);
            if (conn != null && conn.connected)
            {
                stream = new NetStream(conn);
                stream.checkPolicyFile=true;
                stream.bufferTime=3;
                
                return stream;
            }
            return null;
        }
        
        /**
         * 
         * @param ns
         * 
         */        
        private static function destroyStream(ns:NetStream):void{
        	if (ns)
            {
                ns.close();
            }
        }
        
        /**
         *	播放一个流; 
         * @param video
         * @param stream
         * 
         */        
        static public function showVideo(video:Video, stream:*) : void
        {
            if (stream != null && video != null)
            {
                video.visible = true;
                if (stream is Camera)
                {
                    video.attachCamera(Camera(stream));
                }
                else if (stream is NetStream)
                {
                    video.attachNetStream(NetStream(stream));
                }
            }
        }
        
        /**
         * 关闭一个视频; 
         * @param param1
         * 
         */        
        static public function closeVideo(video:Video) : void
        {
            if (video != null)
            {
                video.attachCamera(null);
                video.attachNetStream(null);
                video.clear();
            }
            if(video.visible)video.visible=false;
        }
        
        
        static public function closeStream(stream:NetStream):void{
        	if(stream){
        		stream.attachAudio(null)
        		stream.attachCamera(null);
        		stream.close();
        	}
        }

	}
}